package com.xxr.mom.ci.charging.interworking.v2016.accepter.query;

import com.xxr.mom.ci.charging.bill.dto.HlhtAuthRequest;
import com.xxr.mom.ci.charging.bill.dto.HlhtAuthResponse;
import com.xxr.mom.ci.charging.bill.feign.ChargingFeign;
import com.xxr.mom.ci.charging.device.entity.Gun;
import com.xxr.mom.ci.charging.device.entity.Pile;
import com.xxr.mom.ci.charging.device.entity.Station;
import com.xxr.mom.ci.charging.interworking.base.InterfaceAccepter;
import com.xxr.mom.ci.charging.interworking.dto.InterworkingConfigDto;
import com.xxr.mom.ci.charging.interworking.v2016.AbstractV2016Service;
import com.xxr.mom.ci.charging.interworking.vo.api.QueryEquipAuthRequest;
import com.xxr.mom.ci.charging.interworking.vo.api.QueryEquipAuthResponse;
import com.xxr.mom.ci.charging.mapper.device.GunMapper;
import com.xxr.mom.ci.charging.mapper.device.PileMapper;
import com.xxr.mom.ci.charging.mapper.device.StationMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * 请求设备认证
 * 概述：此接口用于客户归属运营商请求充电基础设施的认证信息
 * 接口名称：query_equip_auth
 * 接口使用方法：由基础设施运营商服务平台实现此接口，客户归属运营商服务平台方调用
 *
 * @description: QueryEquipAuthService
 * @date: 2024/8/12 20:44
 * @author: natsuki_kining
 * @version: 1.0
 */
@Service
public class QueryEquipAuthAccepterService extends AbstractV2016Service<QueryEquipAuthRequest, QueryEquipAuthResponse> implements InterfaceAccepter<QueryEquipAuthRequest, QueryEquipAuthResponse> {

    @Autowired(required = false)
    private ChargingFeign chargingFeign;

    @Autowired
    private GunMapper gunMapper;
    @Autowired
    private PileMapper pileMapper;
    @Autowired
    private StationMapper stationMapper;

    @Override
    public QueryEquipAuthResponse dataHandler(QueryEquipAuthRequest queryEquipAuthRequest, InterworkingConfigDto configDto) {
        QueryEquipAuthResponse queryEquipAuthResponse = new QueryEquipAuthResponse();
        queryEquipAuthResponse.setEquipAuthSeq(queryEquipAuthRequest.getEquipAuthSeq());
        queryEquipAuthResponse.setConnectorId(queryEquipAuthRequest.getConnectorId());

        String connectorId = queryEquipAuthRequest.getConnectorId();
        Gun gun = gunMapper.selectByConnectorId(connectorId);
        if (Objects.isNull(gun)) {
            queryEquipAuthResponse.setSuccStat(1);
            queryEquipAuthResponse.setFailReason(2);
            return queryEquipAuthResponse;
        }
        Pile pile = pileMapper.selectById(gun.getPileId());
        Station station = stationMapper.selectById(gun.getStationId());

        HlhtAuthRequest hlhtAuthRequest = new HlhtAuthRequest();
        hlhtAuthRequest.setEquipAuthSeq(queryEquipAuthRequest.getEquipAuthSeq());
        hlhtAuthRequest.setGun(gun);
        hlhtAuthRequest.setPile(pile);
        hlhtAuthRequest.setStation(station);

        HlhtAuthResponse hlhtAuthResponse = chargingFeign.hlhtAuth(hlhtAuthRequest);

        queryEquipAuthResponse.setSuccStat(hlhtAuthResponse.getCode());
        queryEquipAuthResponse.setFailReason(hlhtAuthResponse.getFailReason());

        return queryEquipAuthResponse;
    }

}
